import requests
import json
import time

class SDK:
    def __init__(self,url,username=None,password=None):
        self.url = url;
        self.headers = {'content-type': 'application/json'}
        self.id = 0;
    def call(self,func,params):
        self.id = self.id+1;
        req_id = self.id;
        payload = {
            "method":func,
            "params": params,
            "jsonrpc": "2.0",
            "id": req_id,
        }
        response = requests.post(self.url, data=json.dumps(payload), headers=self.headers).json()
        if 'error' in response:
            raise Exception(response['error']);
        return response['result']
    def __getattr__(self,name):
        if name in self.__dict__:
            return self.__dict__[name]
        def func(*args):
            return self.call(name,args)
        return func

    def get_all(self,fields = '*'):
        return self.call('get_all',[fields]);

    def get_trade_date(self,start,end,exchange='sh'):
        return self.call('get_trade_date',[exchange,start,end]);

    def get_daily_n_bars(self,symbol, num,endDate,adjType = 'none'):
        return self.call('get_daily_n_bars',[symbol, num,endDate, adjType]);

    def get_daily_bars(self,symbol, start, end, adjType = 'hfq'):
        return self.call('get_daily_bars',[symbol, start, end, adjType]);

    '''
    获取某日上市的股票代码列表
    '''
    def get_all_symbols_by_date(self,date):
        return self.call('get_all_symbols_by_date',[date]);

    '''
    获取某日上市的股票最新行情
    '''
    def get_all_by_date(self,date):
        return self.call('get_all_by_date',[date]);


    def get_bars_by_date(self,date):
        return self.call('get_quotations_of_date',[date]);


